#include <iostream>
#include <vector>
#include <iomanip>
#include "Spline.hpp" 

int main() {
    // 定义节点和对应的函数值
    std::vector<double> x = {0.0, 1.0, 2.0, 3.0, 4.0};  // 节点
    std::vector<double> y = {0.0, 1.0, 0.5, 1.5, 0.0};  // 函数值
    // Create a LinearPPForm object
    LinearPPForm linear_spline(x, y);

    // Test points for interpolation
    std::vector<double> test_points = {0.5, 1.5, 2.5, 3.5};
    std::cout << "Test1(Linear Spline):" << std::endl;
    // Output the spline values at test points
    std::cout << "Linear PPform Spline Interpolation Test Results:" << std::endl;
    for (double x : test_points) {
        double value = linear_spline.getSplineValue(x);
        std::cout << "Spline value at x = " << x << " : " << value << std::endl;
    }

    // 创建 LinearBSpline 对象
    LinearBSpline linearBSpline(x, y);
    std::cout << "Testing Linear BSpline Interpolation Test Results:" << std::endl;
    std::cout << std::fixed << std::setprecision(6);

    for (double t : test_points) {
        double interpolated_value = linearBSpline.getSplineValue(t);
        std::cout << "Spline value at x = " << t << " : " << interpolated_value << std::endl;
    }

    // 测试边界点的插值
    std::cout << "\nBoundary tests:" << std::endl;
    std::cout << "x = " << x.front() << ", Interpolated value = " << linearBSpline.getSplineValue(x.front()) << std::endl;
    std::cout << "x = " << x.back() << ", Interpolated value = " << linearBSpline.getSplineValue(x.back()) << std::endl;

    // 测试超出范围的情况
    try {
        std::cout << "\nTesting out-of-range value:" << std::endl;
        double out_of_range_value = linearBSpline.getSplineValue(-1.0);
        std::cout << "x = -1.0, Interpolated value = " << out_of_range_value << std::endl;
    } catch (const std::exception &e) {
        std::cerr << "Error: (x = -1.0)" << e.what() << std::endl;
    }

    try {
        double out_of_range_value = linearBSpline.getSplineValue(5.0);
        std::cout << "x = 5.0, Interpolated value = " << out_of_range_value << std::endl;
    } catch (const std::exception &e) {
        std::cerr << "Error: (x = 5.0)" << e.what() << std::endl;
    }

    return 0;
}
